import { request, useUserStore, clearEmpty } from "jmash-core";
import type { AxiosPromise } from "axios";
import type {
  ResourceReq,
  ResourceApi,
  ResourceModel,
  ResourceKey,
  ResourcePage,
  ResourceCreateReq,
  ResourceUpdateReq,
  ResourceKeyList,
  ResourceMoveKey,
  ResourceExportReq,
  ResourceImportReq,
} from "./types";
import qs from "qs";

class ResourceApiImpl implements ResourceApi {
  getKey(model: ResourceModel): ResourceKey {
    return {
      resourceId: model.resourceId,
    } as ResourceKey;
  }

  /**
   * 获取页面数据
   *
   * @param queryParams 查询参数
   * @returns 返回请求结果
   */
  findPage(query: ResourceReq): AxiosPromise<ResourcePage> {
    query.tenant = useUserStore().user.tenant;
    if (query) {
      query.hasHidden = query.hidden != undefined;
    }
    clearEmpty(query);
    return request({
      url: "/v1/rbac/resource/list",
      method: "get",
      params: query,
    });
  }

  // 查询资源表
  findById(query: ResourceKey): AxiosPromise<ResourceModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/resource/id",
      method: "get",
      params: query,
    });
  }

  /**
   * 添加资源
   *
   * @param data 资源数据
   * @returns 返回请求结果
   */
  create(data: ResourceCreateReq): AxiosPromise<ResourceModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/resource",
      method: "post",
      data: data,
    });
  }

  /**
   * 编辑资源
   *
   * @param data 更新后的数据
   * @returns 返回请求的响应结果
   */
  update(data: ResourceUpdateReq): AxiosPromise<ResourceModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/resource",
      method: "patch",
      data: data,
    });
  }

  /**
   * 批量删除资源
   *
   * @param ids 资源ID数组
   * @returns 返回请求结果
   */
  batchDelete(keys: ResourceKey[]): AxiosPromise<number> {
    const ids = keys.map((item) => item.resourceId);
    const data: ResourceKeyList = {
      tenant: useUserStore().user.tenant,
      resourceId: ids,
    } as ResourceKeyList;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/resource/batch",
      method: "put",
      data: data,
    });
  }

  /**
   * 删除资源
   *
   * @param resourceId 资源ID
   * @returns 返回请求结果
   */
  delete(query: ResourceKey): AxiosPromise<ResourceModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/resource/id",
      method: "delete",
      params: query,
    });
  }

  /**
   * 向上/下移动资源
   *
   * @param roleId 资源ID
   * @returns 返回Promise对象，表示异步操作的结果
   */
  move(key: ResourceKey, up: boolean): AxiosPromise<boolean> {
    const data: ResourceMoveKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.up = up;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/resource/move",
      method: "put",
      data: data,
    });
  }
  /**
   * 下载资源模板
   *
   * @returns 返回请求的 Promise 对象
   */

  downloadTemplate(): AxiosPromise {
    return request({
      url: "/v1/rbac/resource/template.xlsx",
      method: "get",
      responseType: "blob",
      params: {
        tenant: useUserStore().user.tenant,
      },
    });
  }

  /**
   * 导入资源
   *
   * @param data 资源数据
   * @returns 返回请求结果
   */

  importExcel(data: ResourceImportReq): AxiosPromise<string> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/resource/import",
      method: "patch",
      data: data,
    });
  }

  /**
   * 导出资源到Excel文件
   *
   * @param queryParams 查询参数
   * @returns 返回一个Promise对象，用于处理异步操作
   */
  exportExcel(queryParams: ResourceExportReq): AxiosPromise {
    clearEmpty(queryParams);
    const query = qs.stringify(queryParams, {
      arrayFormat: "brackets",
      allowDots: true,
    });
    return request({
      url:
        "/v1/rbac/resource/" +
        useUserStore().user.tenant +
        "/resources.xlsx?" +
        query,
      method: "get",
      responseType: "blob",
    });
  }

  /**
   * 获取树形列表
   *
   * @param resourceId 排除的资源ID
   * @returns 返回树形列表数据
   */
  treelist(resourceId?: string): AxiosPromise {
    const query = {
      tenant: useUserStore().user.tenant,
      excludeId: resourceId,
    };
    return request({
      url: "/v1/rbac/resource/treelist",
      method: "get",
      params: query,
    });
  }

  /**
   * 判断菜单是否存在
   *
   * @param url 菜单url
   * @returns 返回请求结果
   */
  existMenu(url: string): AxiosPromise {
    const query = {
      tenant: useUserStore().user.tenant,
      url: url,
    };
    return request({
      url: "/v1/rbac/resource/exist",
      method: "get",
      params: query,
    });
  }
  /**
   * 获取操作列表
   *
   * @returns 返回请求结果
   */
  operList(): AxiosPromise {
    return request({
      url: "/v1/rbac/operation/list?tenant=" + useUserStore().user.tenant,
      method: "get",
    });
  }
  /**
   * 获取资源ID
   *
   * @param resourceId 资源ID
   * @returns 返回请求结果
   */
  resourceId(resourceId: string): AxiosPromise {
    const query = {
      tenant: useUserStore().user.tenant,
      resourceId: resourceId,
    };
    return request({
      url: "/v1/rbac/resource/id",
      method: "get",
      params: query,
    });
  }
}
const resourceApi = new ResourceApiImpl();
export { resourceApi };
